/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.ipti.br.tag.bean.questionnaire;

import org.ipti.br.tag.bean.AppBaseBean;
import org.ipti.br.tag.bean.SessionBean;
import org.ipti.br.tag.bean.dataManipulation.SelectionElement;
import org.ipti.br.tag.bo.CampaignBO;
import org.ipti.br.tag.bo.HbBO;
import org.ipti.br.tag.bo.HumanBO;
import org.ipti.br.tag.bo.UnityBO;
import org.ipti.br.tag.entity.Campaign;
import org.ipti.br.tag.entity.Human;
import org.ipti.br.tag.entity.Unity;
import javax.inject.Named;
import java.io.Serializable;
import java.util.List;
import javax.faces.view.ViewScoped;
import javax.inject.Inject;

/**
 *
 * @author luancorumba
 */
@Named @ViewScoped
public class ConsultationMB extends AppBaseBean implements Serializable {

    @Inject private SessionBean sessionBean;
    @Inject private CampaignBO campaignBO;
    @Inject private UnityBO unityBO;
    @Inject private HumanBO humanBO;
    @Inject private HbBO hbBO;
    
    private List<SelectionElement<Human,Boolean>> humansStatus;
    private Campaign campaign;
    private Unity unity;
    private Integer year = this.getActualYear();
    
    public void save() {
        String[] errors;
        for (SelectionElement<Human, Boolean> selectionElement : humansStatus) {
            errors = hbBO.saveConsultation(selectionElement, campaign);
            if (errors != null) {
                this.addErrorMessage(":selectionF:msg", errors[0],errors[1]);
            }
        }
        this.addInfoMessage(":selectionF:msg", "Suas informações foram salvas", ":D");
    }
    
    public void loadHumansStatus() {
        if(campaign != null && unity != null) {
            humansStatus = humanBO.getHumanDAO().listAnemicsStatus(campaign, unity);
        }
    }
    
    public List<Campaign> getCampaigns() {
        List<Campaign> list = campaignBO.findAll();
        campaign = (list.size() > 0) ? list.get(0) : null;
        return list;
    }
    
    public List<Unity> getUnityList() {
        if (this.campaign != null) {
            return unityBO.getClassLessonList(sessionBean.getLoggedActor().getUnity(),year);
        } else {
            return null;
        }
    }
    
    public List<SelectionElement<Human,Boolean>> getHumansStatus() {
        return humansStatus;
    }

    public void setHumanStatus(List<SelectionElement<Human, Boolean>> humanStatus) {
        this.humansStatus = humanStatus;
    }

    public Campaign getCampaign() {
        return campaign;
    }

    public void setCampaign(Campaign campaign) {
        this.campaign = campaign;
    }

    public Unity getUnity() {
        return unity;
    }

    public void setUnity(Unity unity) {
        this.unity = unity;
    }

    public Integer getYear() {
        return year;
    }

    public void setYear(Integer year) {
        this.year = year;
    }
}
